C#_c#编写的高并发数据库控制访问代码,代码的作用在于保证在上端缓
一点思考:
平均会有 10000×0.001=10条记录会执行到上述这段代码,操作系统会为锁形成等待序列。
{
finally
currentValidSessionID从-1变为其他值的时间为0.001ms,这个时间内,
修改内存变量需要耗时:0.001ms;
复制代码 代码如下:
复制代码 代码如下:
}currentValidSessionID = currentRequest.SessionID;
if (currentValidSessionID == -1)
if (currentValidSessionID == -1)
假设(非完全正确数据,仅做示例):
那么这就是
if (currentValidSessionID == -1)
// exception codes go here
{
{
}
}
// use currentValidSessionID to filter out other requests came in during the execute time gap
那么我们的目标是,每毫秒只允许一次读库(因为其他应用也会使用),所以我们只希望这进入的10条,最终只有一条能够继续前进。
代码的作用在于保证在上端缓存服务失效(一般来说概率比较低)时,形成倒瓶颈,从而能够保护数据库,数据库宕了,才是大问题(比如影响其他应用)。
}if (currentValidSessionID == -1)
catch()
}
微观到1ms来看,在currentValidSessionID == -1的时间是 1ms,从而平均会有10000条记录涌入。
public static object databaseDoor = new object();
}
}
{
那么:
每秒支持10,000,000次查询(千万);
{ // now there will be only one request can access the database
}
{ // here is the one !
// use transaction to guarantee the execute time to void block
{
一次读库需要耗时:1ms;
{
void readDatabase(Request currentRequest)
的作用了。再次进行一次判断,进入原子保护队列的请求,也只有一个能够继续。
lock (databaseDoor)不过一个架构师,如果可以用一个99.99%安全的方案,就绝对不用99.9%。 SO。 // now there is only very little number of requests can reach below codes.
{
if (currentValidSessionID == currentRequest.SessionID)
currentValidSessionID = -1; // recover to original state
// access database codes go here
其他的9,900,000个请求会返回到其他页面。这就是为啥很多抢单网站有人可以访问,而有人得到繁忙中页面的原因。
lock (databaseDoor)
// use object-lock to filter out other requests came in during the variable change time gap.
{
// now there is only one request can reach below codes.
其实对于一个主频能上N GHz的服务器来说,一个内存数赋值给另一个内存数据就是1~4条指令(平均2条,两次MOV操作),也就是2/N ns时间,而不是我们上述假设的 1000ns(0.001ms)。其实不用原子,我们已经可以把千亿级请求的访问数控制在个位数。
public static long currentValidSessionID = -1;
{
try
复制代码 代码如下:
相关热词: C#
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/c/5773.shtml
